/*
 * @Author: Gavin
 * @Date: 2018-09-07 09:58:56
 * @LastEditors: Gavin
 * @LastEditTime: 2018-09-07 17:14:26
 * @Description:
 */
import path from 'path'

import { baseconfig, outputConfig } from './webpack-base'
import rulesConfig from './webpack.rules'
import pluginsConfig from './webpack.plugins'
import { splitChunks, minimizer } from './webpack.optimization'
import { APP_PATH, BUILD_PATH } from '../../config/paths'

const webConfig = {
    target: 'web',
    optimization: {
        splitChunks: splitChunks
    }
}

export const devConfig = Object.assign({}, baseconfig, webConfig, {
    // devtool: 'source-map',
    mode: 'development',
    entry: ['webpack-hot-middleware/client?noInfo=true&reload=true', path.resolve(APP_PATH, 'client.dev.js')],
    output: Object.assign({}, outputConfig, {
        publicPath: "/assets/",
    }),
    watch: true, // 开启监听文件更改，自动刷新
    watchOptions: {
        ignored: /node_modules/, //忽略不用监听变更的目录
        aggregateTimeout: 300, //防止重复保存频繁重新编译,500毫米内重复保存不打包
        poll: 1000 //每秒询问的文件变更的次数
    },
    optimization: Object.assign({}, webConfig.optimization, {
        namedModules: true,
    }),
    module: {
        rules: rulesConfig(false)
    },
    plugins: pluginsConfig(false)
})

export const prodConfig = Object.assign({}, baseconfig, webConfig, {
    devtool: false,
    mode: 'production',
    entry: path.resolve(APP_PATH, 'client.prod.js'),
    output: Object.assign({}, outputConfig, {
        path: path.join(BUILD_PATH, "assets")
    }),
    optimization: Object.assign({}, webConfig.optimization, {
        noEmitOnErrors: true,
        namedChunks: true,
        minimizer: minimizer
    }),
    module: {
        rules: rulesConfig(true)
    },
    plugins: pluginsConfig(true)
})



export default [devConfig, prodConfig]
